import React, { useState } from 'react';
import { Drawer, Tree,Button, message } from 'antd';
import {treeData} from "../config/treeData"
import {roleAuth} from "../apis/role"
const MyDrawer = ({ setOpen,role,fetchData }) => {
    const [selectMenus,setSelectMenus] = useState([])
    const onCheck = (checkedKeys, info) => {
        console.log(checkedKeys);
        setSelectMenus(checkedKeys)
    };

    const onClose = () => {
        setOpen(false)
    };

    const updateRoleAuth =async ()=>{
        let obj = {
            id:role._id,
            authTime:new Date().toLocaleString(),
            authUser:"xiaowang",
            menus:selectMenus
        }
        console.log(obj);
        const res = await roleAuth(obj)
        if(res.data.code){
            message.success("授权成功")
            setOpen(false)
            fetchData()
        }else{
            message.error("授权失败")
        }


    }
    return (
        <>
            <Drawer title="权限控制" onClose={onClose} open={true}>
                <Tree
                    checkable
                    defaultExpandedKeys={['/home/product',"/home/chart"]}
                    defaultCheckedKeys={role.menus}
                    onCheck={onCheck}
                    treeData={treeData}
                />
                <Button onClick={updateRoleAuth}>授权</Button>
            </Drawer>
        </>
    );
};
export default MyDrawer;